package com.sduept.nwld.dataserver.controller.statistics;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.primefaces.event.SelectEvent;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.manager.statistics.TransmissionLineManager;
import com.sduept.nwld.dataserver.model.countanalyse.TansmissionLineInfo;
import org.springframework.beans.factory.annotation.Autowired;

@Named
@ViewScoped
public class TransmissionLineController extends AbstractController {

	private static final long serialVersionUID = 258471919844760062L;
	@Autowired
	private TransmissionLineManager manager;

	private List<TansmissionLineInfo> tansmissionLineInfos = new ArrayList<TansmissionLineInfo>();
	private TansmissionLineInfo current = new TansmissionLineInfo();
	private TansmissionLineInfo select = new TansmissionLineInfo();
	private String voltage;
	private String year;


	@PostConstruct
	public void init() {
		tansmissionLineInfos = manager.getAllTansmissionLineInfo();
	}

	public void preCreat() {
		current = new TansmissionLineInfo();
		year = null;
		voltage = null;
	}

	public void preEdit() {
		current = select;
		if(null != current.getYear()) {
			year = current.getYear().toString();
		}
		if(null != current.getVoltage()) {
			voltage = current.getVoltage().toString();
		}
	}

	public void saveTansmissionLineInfo() {
		if(current != null) {
			if(null != year) {
				current.setYear(Integer.parseInt(year));
			}
			if(null != voltage && !"0".equals(voltage)) {
				current.setVoltage(Integer.parseInt(voltage));
			}
			if(current.getId() == null) {
				// 新建
				if(!isExist(current)) {
					countNewAddLength(current);
				} else {
					addErrorMessage("该条件下数据已存在！");
					return;
				}
			} else {
				// 编辑
				countNewAddLength(current);
				//				manager.createOrUpdateTansmissionLineInfo(current);
				select = new TansmissionLineInfo();
			}
			addSuccessMessage("保存成功");
		}
		tansmissionLineInfos = manager.getAllTansmissionLineInfo();
	}

	private boolean isExist(TansmissionLineInfo current) {
		List<TansmissionLineInfo> lt = manager.getTansmissionLineInfos(current.getYear(), current.getVoltage());
		if(lt.size()>0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 为计算新增长度，获取当前记录的前一条和后一条记录
	 * @param current
	 * @return
	 */
	private TansmissionLineInfo[] getPreAndNextInfo(TansmissionLineInfo current) {
		Integer voltage = current.getVoltage();
		List<TansmissionLineInfo> list = manager.getTansmissionLineInfos(null, voltage);
		TansmissionLineInfo pre = null;
		TansmissionLineInfo next = null;
		if(!list.contains(current)) {
			list.add(current);
		}
		if(list.size()>1) {
			sortListByYear(list);
			int index = list.indexOf(current); // 当前记录在list中的位置
			// 获取前一条数据
			if(index > 0) {
				pre = list.get(index-1);
			}
			// 获取后一条数据
			if(index < list.size()-1) {
				next = list.get(index+1);
			}
		}
		TansmissionLineInfo[] obj = {pre,next};
		return obj;
	}

	/**
	 * 计算当前线路与上一年线路的新增长度
	 * @param current
	 */
	private void countNewAddLength(TansmissionLineInfo current) {
		TansmissionLineInfo[] obj = getPreAndNextInfo(current);
		TansmissionLineInfo pre = obj[0];
		TansmissionLineInfo next = obj[1];
		getNewAddlengthBetweenTwoRecords(pre, current);
		if(null != pre) {
			manager.createOrUpdateTansmissionLineInfo(pre);
		}
		getNewAddlengthBetweenTwoRecords(current, next);
		if(null != next) {
			manager.createOrUpdateTansmissionLineInfo(next);
		}
		manager.createOrUpdateTansmissionLineInfo(current);
	}

	/**
	 * 计算两条记录之间的新增长度
	 * @param pre
	 * @param current
	 */
	private void getNewAddlengthBetweenTwoRecords(TansmissionLineInfo pre,TansmissionLineInfo current) {
		if(null == pre) {
			current.setIncreaseLength(0D);
		}
		if(null != pre && null !=current) {
			Double newAdd = current.getLength() - pre.getLength();
			current.setIncreaseLength(newAdd);
		}
	}

	private void sortListByYear(List<TansmissionLineInfo> list) {
		// 重新排序
		Collections.sort(list, new Comparator<TansmissionLineInfo>() {
			@Override
			public int compare(TansmissionLineInfo o1, TansmissionLineInfo o2) {
				return o1.getYear()-o2.getYear();
			}
		});
	}

	public void deleteTansmissionLineInfo() {
		if(select != null && !select.getId().isEmpty()) {
			updateNextWhenDelete(select);
			manager.deleteTansmissionLineInfo(select.getId());
			select = null;
			addSuccessMessage("删除成功！");
		} else {
			addErrorMessage("请首先选择记录！");
		}
		tansmissionLineInfos = manager.getAllTansmissionLineInfo();
	}

	private void updateNextWhenDelete(TansmissionLineInfo current) {
		TansmissionLineInfo[] obj = getPreAndNextInfo(current);
		TansmissionLineInfo pre = obj[0];
		TansmissionLineInfo next = obj[1];
		if(null != next) {
			getNewAddlengthBetweenTwoRecords(pre,next);
			manager.createOrUpdateTansmissionLineInfo(next);
		}
	}

	public void onRowSelect(SelectEvent event) {
		select = (TansmissionLineInfo) event.getObject();
	}

	public List<TansmissionLineInfo> getTansmissionLineInfos() {
		return tansmissionLineInfos;
	}

	public void setTansmissionLineInfos(
			List<TansmissionLineInfo> tansmissionLineInfos) {
		this.tansmissionLineInfos = tansmissionLineInfos;
	}

	public TansmissionLineInfo getSelect() {
		return select;
	}

	public void setSelect(TansmissionLineInfo select) {
		this.select = select;
	}

	public String getVoltage() {
		return voltage;
	}

	public void setVoltage(String voltage) {
		this.voltage = voltage;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public TansmissionLineInfo getCurrent() {
		return current;
	}

	public void setCurrent(TansmissionLineInfo current) {
		this.current = current;
	}
	//
	//	public static void main(String[] args) {
	//		int num = 8;
	//		if(num%2 == 0) {
	//			// 可以被2整除
	//			int count = 0;
	//			for(int i = num; i == 0; i=i/2) {
	//				count++;
	//				log.info(count);
	//			}
	//		}
	//	}
	//
}
